package com.lcgs.manage.service.impl;

import java.util.List;
import com.lcgs.common.utils.DateUtils;
import com.lcgs.manage.domain.Product;
import com.lcgs.manage.mapper.ProductMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.lcgs.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.lcgs.manage.domain.TbOrderDetail;
import com.lcgs.manage.mapper.OrderMapper;
import com.lcgs.manage.domain.Order;
import com.lcgs.manage.service.IOrderService;

/**
 * 货单管理Service业务层处理
 * 
 * @author lc
 * @date 2025-09-20
 */
@Service
public class OrderServiceImpl implements IOrderService 
{
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private ProductMapper productMapper;

    /**
     * 查询货单管理
     * 
     * @param orderId 货单管理主键
     * @return 货单管理
     */
    @Override
    public Order selectOrderByOrderId(Long orderId)
    {
        return orderMapper.selectOrderByOrderId(orderId);
    }

    /**
     * 查询货单管理列表
     * 
     * @param order 货单管理
     * @return 货单管理
     */
    @Override
    public List<Order> selectOrderList(Order order)
    {
        return orderMapper.selectOrderList(order);
    }

    /**
     * 新增货单管理
     * 
     * @param order 货单管理
     * @return 结果
     */
    @Transactional
    @Override
    public int insertOrder(Order order)
    {
        order.setCreateTime(DateUtils.getNowDate());
        int rows = orderMapper.insertOrder(order);
        //获取订单详情商品列表，根据数量减少库存
        List<TbOrderDetail> tbOrderDetailList = order.getTbOrderDetailList();
        for (TbOrderDetail tbOrderDetail : tbOrderDetailList) {
            Product product = productMapper.selectProductByProductId(tbOrderDetail.getProductId());
            product.setStock(product.getStock() - tbOrderDetail.getNumber());
            if (product.getStock() < 0)
                throw new RuntimeException("商品库存不足！");

            if (product.getStock() == 0)
                product.setStatus(0);
            productMapper.updateProduct(product);
        }
        insertTbOrderDetail(order);
        return rows;
    }

    /**
     * 修改货单管理
     * 
     * @param order 货单管理
     * @return 结果
     */
    @Transactional
    @Override
    public int updateOrder(Order order)
    {
        order.setUpdateTime(DateUtils.getNowDate());
        return orderMapper.updateOrder(order);
    }

    /**
     * 批量删除货单管理
     * 
     * @param orderIds 需要删除的货单管理主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteOrderByOrderIds(Long[] orderIds)
    {
        orderMapper.deleteTbOrderDetailByOrderIds(orderIds);
        return orderMapper.deleteOrderByOrderIds(orderIds);
    }

    /**
     * 删除货单管理信息
     * 
     * @param orderId 货单管理主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteOrderByOrderId(Long orderId)
    {
        orderMapper.deleteTbOrderDetailByOrderId(orderId);
        return orderMapper.deleteOrderByOrderId(orderId);
    }

    /**
     * 新增订单详情信息
     * 
     * @param order 货单管理对象
     */
    public void insertTbOrderDetail(Order order)
    {
        List<TbOrderDetail> tbOrderDetailList = order.getTbOrderDetailList();
        Long orderId = order.getOrderId();
        if (StringUtils.isNotNull(tbOrderDetailList))
        {
            List<TbOrderDetail> list = new ArrayList<TbOrderDetail>();
            for (TbOrderDetail tbOrderDetail : tbOrderDetailList)
            {
                tbOrderDetail.setOrderId(orderId);
                list.add(tbOrderDetail);
            }
            if (list.size() > 0)
            {
                orderMapper.batchTbOrderDetail(list);
            }
        }
    }
}
